//
//  LPNewsCommentCoustomView.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 2017/6/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPNewsCommentCoustomView.h"

@interface LPNewsCommentCoustomView ()
/** 评论输入框 */
@property (nonatomic, strong) UIButton *commentTextButton;
/**  输入框的宽度  */
@property (assign, nonatomic) CGFloat  textButtonWidth;
/** 分割线 */
@property (nonatomic, strong) UILabel *spaceLine;
/**  titles  */
@property (strong, nonatomic) NSArray  *buttonTitles;
/**  images  */
@property (strong, nonatomic) NSArray  *buttonImages;


@end
@implementation LPNewsCommentCoustomView
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles images:(NSArray *)images
{
    self = [super initWithFrame:frame];
    if (self) {
        self.buttonTitles = titles;
        self.buttonImages = images;
        
        if (titles.count == 3) {
            self.textButtonWidth = 190 * [UIScreen zj_screenWScale];
        }else if(titles.count == 2){
            self.textButtonWidth = 220 * [UIScreen zj_screenWScale];
        }else{
            self.textButtonWidth = 260 * [UIScreen zj_screenWScale];
        }
        
        [self addSubview:self.commentTextButton];
        [self addSubview:self.spaceLine];
        [self layoutUI];
        self.backgroundColor = [UIColor whiteColor];

    }
    return self;
}


- (void)layoutUI{
    CGFloat width = ([UIScreen zj_screenWith] - self.commentTextButton.right - 36 * [UIScreen zj_screenWScale]) / self.buttonTitles.count;
    for (int i = 0;  i < self.buttonTitles.count; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((self.commentTextButton.right + 18 * [UIScreen zj_screenWScale]) + i * width, 0, width, 49)];
        [button setImage:[UIImage imageNamed:self.buttonImages[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:self.buttonImages[i]] forState:UIControlStateHighlighted];
        [button setTitle:self.buttonTitles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor textBlackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:10];
        button.tag = i;
        [button addTarget:self action:@selector(action_commentButton:) forControlEvents:UIControlEventTouchUpInside];
        [self setButtonUI:button];
        [self addSubview:button];
        if ([self.buttonTitles[i] isEqualToString:@"报名"] || [self.buttonTitles[i] isEqualToString:@"签到"]) {
            self.signButton = button;
        }
    }
}

- (void)action_commentTextButton:(UIButton *)sender{
    if (self.delegate) {
        [self.delegate LPNEWSCommentCoustomViewDelegate_commentTextButtonClick];
    }
}

- (void)action_commentButton:(UIButton *)sender{
    if (self.delegate) {
        [self.delegate LPNEWSCommentCoustomWithView:self commentButtonClick:sender.tag];
    }

}


- (void)setCommentTextString:(NSString *)commentTextString{
    _commentTextString = commentTextString;
    [self.commentTextButton setTitle:commentTextString forState:UIControlStateNormal];

}
- (void)setButtonUI:(UIButton *)button{
    
    button.titleEdgeInsets = UIEdgeInsetsMake(25 * [UIScreen zj_screenHScale], -button.imageView.image.size.width, 0.0, 0.0);
    button.imageEdgeInsets = UIEdgeInsetsMake(- 15 * [UIScreen zj_screenHScale], (button.bounds.size.width - button.imageView.image.size.width)/2, 0.0,(button.bounds.size.width - button.imageView.image.size.width)/2);
}


- (UIButton *)commentTextButton{
    if (_commentTextButton == nil){
        _commentTextButton = [[UIButton alloc]initWithFrame:CGRectMake(18 * [UIScreen zj_screenWScale], 10, self.textButtonWidth, 29)];
        _commentTextButton.backgroundColor = [UIColor zj_colorWithHexString:@"f3f3f3"];
        _commentTextButton.layer.cornerRadius = 8 ;
        _commentTextButton.titleLabel.font = [UIFont systemFontOfSize:14 * [UIScreen zj_textFontScale]];
        [_commentTextButton setTitle:@"期待您的评论" forState:UIControlStateNormal];
        [_commentTextButton setTitleColor:[UIColor zj_colorWithHexString:@"777777"] forState:UIControlStateNormal];
        [_commentTextButton addTarget:self action:@selector(action_commentTextButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _commentTextButton;
}


- (UILabel *)spaceLine{
    if (_spaceLine == nil){
        _spaceLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 0.5)];
        _spaceLine.backgroundColor = [UIColor zj_colorWithHexString:@"dedede"];
    }
    return _spaceLine;
}

@end
